<template>
	<el-container>
        <el-header class="header-tabs">
			<el-tabs type="card" v-model="groupId" @tab-change="tabChange">
				<el-tab-pane label="调仓列表" name="1"></el-tab-pane>
				<el-tab-pane label="调仓产品明细" name="2"></el-tab-pane>
			</el-tabs>
		</el-header>
			<el-tabs v-model="statusType" @tab-change="statusChange" v-if="groupId == 1" style="padding-left: 20px;">
                <el-tab-pane label="已审核" name="1"></el-tab-pane>
                <el-tab-pane label="待审核" name="0"></el-tab-pane>
                <el-tab-pane label="全部" name="-1"></el-tab-pane>
            </el-tabs>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" v-auth="'check.add'" @click="add" v-if="groupId == 1"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" v-auth="'check.delete'" :disabled="selection.length == 0" @click="batch_del" v-if="groupId == 1"></el-button>
                <el-button type="warning" plain v-auth="'check.check'" :disabled="selection.length == 0" @click="batch_check" v-if="statusType != 1 && groupId == 1">审核</el-button>
				<el-button type="info" plain v-auth="'check.uncheck'" @click="batch_uncheck" :disabled="selection.length == 0" v-if="statusType != 0 && groupId == 1">反审核</el-button>
                <el-button type="warning" plain v-auth="'check.export'" :disabled="selection.length != 1" @click="exportExport" v-if="groupId == '1'">导出</el-button>
                <sc-export-button :auth="'contract.detailsExport'" :apiObj="shopObj" :search="search" v-if="groupId == 2"></sc-export-button>
                <sc-print-button :auth="'carry.print'" :selection="selection"></sc-print-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-date-picker v-model="search.entry_date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px"/>
					<el-input v-model="search.entry_code" placeholder="请输入入仓单号/唛头" clearable @keyup.enter="upsearch"></el-input>
                    <el-select v-model="search.is_delivery" filterable placeholder="提货状态" v-if="groupId == '2'" clearable >
                        <el-option label="已提货" :value="1" />
                        <el-option label="未提货" :value="0" />
                    </el-select>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding" v-if="groupId == 1">
			<scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border :params="query">
				<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
				<el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
				<el-table-column label="入仓单号/唛头" prop="entry_code" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center">
					<template #default="scope">
						<el-popover placement="top-start" :width="200" trigger="hover" :content="scope.row.remark">
							<template #reference>
								<el-button style="padding: 5px 0;font-size: 12px" text class="m-2">{{ scope.row.remark.substring(0, 10) }}</el-button>
							</template>
						</el-popover>
					</template>
				</el-table-column>
				<el-table-column label="操作人" prop="createStaff.name" align="center"></el-table-column>
				<el-table-column label="操作时间" prop="create_time" align="center"></el-table-column>
				<el-table-column label="审核状态" prop="status" align="center">
					<template #default="scope">
						<el-tag type="warning" v-if="scope.row.status == 0">待审核</el-tag>
						<el-tag type="success" v-if="scope.row.status == 1">通过</el-tag>
						<el-popover placement="top-start" :width="200" trigger="hover" :content="scope.row.reason">
							<template #reference>
								<el-tag type="danger" v-if="scope.row.status == 2">驳回</el-tag>
							</template>
						</el-popover>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="200">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'check.show'">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'check.edit'" v-if="scope.row.status != 1">编辑
							</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="danger" size="small" v-auth="'check.delete'" v-if="scope.row.status != 1">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
        <el-main class="nopadding" v-if="groupId == 2">
            <scTable ref="table" row-key="id" :apiObj="shopObj"  stripe remoteSort remoteFilter border>
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column type="index" width="50" align="center"></el-table-column>
                <el-table-column label="调箱单号" prop="orderinfo.entry_code" align="center"></el-table-column>
                <el-table-column label="入仓单号/唛头" prop="detail.marking" align="center"></el-table-column>
                <el-table-column label="箱号" prop="detail.box_code" align="center"></el-table-column>
                <el-table-column label="品类" prop="goods.cate.cate_name" align="center"></el-table-column>
                <el-table-column label="产品型号" prop="goods.goods_name" align="center"></el-table-column>
                <el-table-column label="总数量" prop="number" align="center"></el-table-column>
                <el-table-column label="状态" align="center">
                    <template #default="scope">
                        <el-tag :type="scope.row.is_delivery == 1 ? 'success' : 'warning'">{{scope.row.is_delivery == 1 ? '已提货' : '未提货' }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center">
                    <template #default="scope">
                        <el-popover placement="top-start" :width="200" trigger="hover" :content="scope.row.remark">
                            <template #reference>
                                <el-button style="padding: 5px 0;font-size: 12px" text class="m-2">{{ scope.row.remark.substring(0, 10) }}</el-button>
                            </template>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column label="操作人" prop="orderinfo.createStaff.name" align="center"></el-table-column>
                <el-table-column label="操作时间" prop="create_time" align="center"></el-table-column>
            </scTable>
        </el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
</template>

<script>
import saveDialog from './save'

export default {
	name: 'BOM列表',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false,
				check: false,
				production: false,
				plan: false,
			},
			apiObj: this.$API.procure.check.list,
            shopObj: this.$API.procure.check.goodslist,
			selection: [],
			search: {
				contract_code: null,
				customer_name: null,
				status: null,
				contract_status: null,
                is_delivery: null
			},
            statusType: "1",
            groupId: "1",
			form: {},
            query: {
                status: 1
            },
		}
	},
	mounted() {

	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.procure.check.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.tableData.splice(index, 1);
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.procure.check.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        async batch_check() {
			var ids = [];
			this.selection.forEach((item) => {
				ids.push(item.id)
			})
			this.$confirm(`确定审核选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var reqData = {id: ids,status:1}
				var res = await this.$API.procure.check.check.post(reqData);
				if (res.code == 1) {
					this.$refs.table.refresh()
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {

			})
		},
        async batch_uncheck() {
			var ids = [];
			this.selection.forEach((item) => {
				ids.push(item.id)
			})
			this.$confirm(`确定反审核选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var reqData = {id: ids}
				var res = await this.$API.procure.check.uncheck.post(reqData);
				if (res.code == 1) {
					this.$refs.table.refresh()
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {

			})
		},
        statusChange(e){
            if(e == -1){
                this.search.status = null
            }else{
                this.search.status = e
            }
            this.upsearch()
        },
        async exportExport(){
            var res = await this.$API.procure.check.export.post({id:this.selection[0].id});
            if (res.code == 1) {
                const fileUrl = res.data;
                const link = document.createElement("a");
                link.href = fileUrl;
                link.download = fileUrl;
                link.click();
            } else {
                this.$alert(res.message, "提示", { type: "error" });
            }
        },
	}
}
</script>

<style></style>
